package com.fh.wanandroid.ui.knowledge.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.fh.wanandroid.R;
import com.fh.wanandroid.ui.mainpager.bean.FeedArticleListData;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KnowledgeListRvAdapter extends RecyclerView.Adapter<KnowledgeListRvAdapter.MyViewHolder> {

    Context context;

    List<FeedArticleListData.DataBean.DatasBean> resultsBeans;


    //实现点击效果
    private ItemClickListener mItemClickListener;

    public KnowledgeListRvAdapter(Context context, List<FeedArticleListData.DataBean.DatasBean> resultsBeans) {

        this.context = context;

        this.resultsBeans = resultsBeans;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_pager, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {


        holder.itemMainPagerAuthor.setText(resultsBeans.get(position).getAuthor());
        holder.itemMainPagerChapterName.setText(resultsBeans.get(position).getChapterName());
        holder.itemMainPagerTitle.setText(resultsBeans.get(position).getTitle());
        holder.itemMainPagerNiceDate.setText(resultsBeans.get(position).getNiceDate());

//        if(resultsBeans.get(position).getTags().get(0).getName().equals("项目")){
//            holder.itemMainPagerTagGreenTv.setVisibility(View.VISIBLE);
//            holder.itemMainPagerTagRedTv.setVisibility(View.GONE);
//        }else{
//            holder.itemMainPagerTagGreenTv.setVisibility(View.GONE);
//            holder.itemMainPagerTagRedTv.setVisibility(View.VISIBLE);
//        }

//        Glide.with(context).
//                load(resultsBeans.get(position).getUrl()).
//                fitCenter().
//                placeholder(R.mipmap.default_dota2).
//                error(R.mipmap.default_dota2).
//        into(holder.ivGirl);
        setClickListener(holder, position);
    }

    private void setClickListener(MyViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null)
                    mItemClickListener.onClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return resultsBeans.size();
    }

    public List<FeedArticleListData.DataBean.DatasBean> getmItems() {
        return resultsBeans;
    }

    public void setmItems(List<FeedArticleListData.DataBean.DatasBean> resultsBeans) {

        this.resultsBeans = resultsBeans;

        this.notifyDataSetChanged();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.item_main_pager_author)
        TextView itemMainPagerAuthor;
        @BindView(R.id.item_main_pager_chapterName)
        TextView itemMainPagerChapterName;
        @BindView(R.id.item_main_pager_title)
        TextView itemMainPagerTitle;
        @BindView(R.id.item_main_pager_like_iv)
        ImageView itemMainPagerLikeIv;
        @BindView(R.id.item_main_pager_niceDate)
        TextView itemMainPagerNiceDate;
        @BindView(R.id.item_main_pager_tag_red_tv)
        TextView itemMainPagerTagRedTv;
        @BindView(R.id.item_main_pager_tag_green_tv)
        TextView itemMainPagerTagGreenTv;
        @BindView(R.id.item_main_pager_group)
        CardView itemMainPagerGroup;
        View itemView;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    public ItemClickListener getmItemClickListener() {

        return mItemClickListener;
    }

    public void setmItemClickListener(ItemClickListener mItemClickListener) {

        this.mItemClickListener = mItemClickListener;
    }

    public interface ItemClickListener {
        void onClick(int position);
    }

}
